<?php

namespace App\Http\Controllers;

use \Session;
use Overtrue\Wechat\Auth;
use Overtrue\Wechat\Server;
use Overtrue\Wechat\Message;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;


class WechatController extends Controller
{

    /**
     * 处理微信的请求消息
     * @return string
     */
    public function serve(Server $server)
    {
//        var_dump('111');die;
        //file_put_contents(base_path()."/public/test.txt",print_r($text,true) ,FILE_APPEND);
        $server->on('message', function($message){
            return "test";
            //$a=json_encode($_GET);
            //return Message::make('text')->content("$a");
        });
        return $server->serve();
    }


    //查询
    public function  GetUserInFo(){
        $action='/GetUserInFo.do';
        $condition=array(
            'openid'=>'ox9a0tx6bkzc2U4loFNq3XDOr08w'
        );
        $getData=getFromApi($action,$condition);
        echo json_encode($getData);
    }

    //绑定
    public function  weChatBind(){
        $action='/weChatBind';
        $condition=array(
            'userid'=>'330184199210032319',
            'phonenum'=>'18868945382',
            'openid'=>'ox9a0tx6bkzc2U4loFNq3XDOr08w'
        );
        $getData=getFromApi($action,$condition);
        echo json_encode($getData);
    }

    //解绑
    public function weChatRemoveBind(){
        $action='/weChatRemoveBind';
        $condition=array(
            'userid'=>'330184199210032319',
            'phonenum'=>'18868945382',
            'openid'=>'ox9a0tx6bkzc2U4loFNq3XDOr08w'
        );
        $getData=getFromApi($action,$condition);
        echo json_encode($getData);
    }


    public function login(){
        /*$a = \App::make('myTest'); // 用法
        echo $a->test();*/
        //$this->weChatBind();
        $this->GetUserInFo();
        //$this->weChatRemoveBind();
    }


    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $user = Session::get('logged_user');
        var_dump($user['openid']);
        var_dump(Session::has('logged_user'));
        if (!Session::has('logged_user')) {
            return redirect('/show/authorize');
        }
     }

    public function authorizes(Auth $auth){
        if (!Session::has('logged_user')) {
            $user = $auth->authorize();
            Session::put('logged_user', $user->all());
        } else {
            return redirect('/show/index');
        }
    }


     /**
      * Show the form for creating a new resource.
      *
      * @return \Illuminate\Http\Response
      */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
